package com.cysd.ewmxns.sdw_utils

import android.graphics.Bitmap
import android.util.Log
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.MultiFormatWriter
import com.journeyapps.barcodescanner.BarcodeEncoder
import java.nio.charset.StandardCharsets

fun generateQRCode(content: String): Bitmap {
    return try {
        val writer = MultiFormatWriter()
        // 添加编码参数
        val hints = mutableMapOf<EncodeHintType, Any>().apply {
            put(EncodeHintType.CHARACTER_SET, "UTF-8")
        }
        val bitMatrix = writer.encode(
            String(content.toByteArray(StandardCharsets.UTF_8), StandardCharsets.UTF_8),
            BarcodeFormat.QR_CODE,
            500,  // 二维码宽度
            500  , // 二维码高度
            hints // 应用编码参数
        )

        BarcodeEncoder().createBitmap(bitMatrix)
    } catch (e: Exception) {
        Log.e("qin", "生成二维码失败: ${e.message}")
        Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888) // 返回空白位图
    }
}